package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.MarketMapper;
import com.aaa.mrcai.pojo.entity.Area;
import com.aaa.mrcai.pojo.entity.Market;
import com.aaa.mrcai.pojo.entity.State;
import com.aaa.mrcai.pojo.vo.MarketVO;
import com.aaa.mrcai.query.MarketQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.MarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:Li-Yan-Chun
 * @Date:2021/12/3 21:19
 */
@Service
public class MarketServiceImpl implements MarketService {

    @Autowired
    private MarketMapper marketMapper;
    @Autowired
    private HttpSession session;
    @Override
    public AjaxResult list(MarketQuery query) {
        List<Market> markets = marketMapper.queryMarket(query);
        Long aLong = marketMapper.queryMarketTotalCount(query);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPageData",markets);
        map.put("currentDataTotal",aLong);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getMarket() {
        List<Market> market = marketMapper.getMarket();
        return AjaxResult.success(market);
    }

    @Override
    public AjaxResult getArea() {
        List<Area> area = marketMapper.getArea();
        return AjaxResult.success(area);
    }

    @Override
    public AjaxResult getState() {
        List<State> state = marketMapper.getState();
        return AjaxResult.success(state);
    }

    @Override
    public AjaxResult addMarket(MarketVO marketVO) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        marketVO.setStartdate(date.format(formatter));
        Long marketExist = marketMapper.isMarketExist(marketVO.getMname(), marketVO.getAid());
        if(marketExist>0){
            return AjaxResult.defeated("对不起，该区域已有该市场");
        }
        int i = marketMapper.addMarket(marketVO);
        if(i<1){
            return AjaxResult.defeated();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getOneMarket(int id) {
        MarketVO oneMarket = marketMapper.getOneMarket(id);
        session.setAttribute("oneMarket",oneMarket);
        return AjaxResult.success(oneMarket);
    }

    @Override
    public AjaxResult updateMarket(MarketVO marketVO) {
        MarketVO oneMarket = (MarketVO) session.getAttribute("oneMarket");
        Integer id = oneMarket.getId();
        if(!marketVO.getId().equals(id)){
            return AjaxResult.defeated("不要乱改代码");
        }
        Long marketIdExist = marketMapper.isMarketIdExist(marketVO);
        if(marketIdExist>0){
            return AjaxResult.defeated("对不起，修改的市场已存在");
        }
        int i = marketMapper.updateMarket(marketVO);
        if (i<0){
            return AjaxResult.defeated();
        }
        return AjaxResult.success();
    }
}
